package com.blds.mc.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;

/**
 * Created by hgg on 2018/2/24.
 * 小程序AES解密
 */
@Slf4j
public class AESDecodeUtils {

    public static void main(String[] args) throws Exception {

    }

    public static String decrypt(byte[] key, byte[] iv, byte[] encData){
        AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv);
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
            //解析解密后的字符串
            return new String(cipher.doFinal(encData), "UTF-8");
        } catch (Exception e) {
            log.error("解析出错：\n", e);
        }

        return null;
    }
}
